package com.zhaoxi.demo.commons;

import com.zhaoxi.demo.models.Student;
import com.zhaoxi.demo.models.StudentQueryFilter;
import org.omg.IOP.CodecPackage.InvalidTypeForEncoding;

import javax.swing.tree.VariableHeightLayoutCache;
import java.io.File;
import java.util.*;

/**
 * Created by Infor on 2017/07/02.
 */
public class VirtualData {

    private static Random random = new Random();
    private static List<Student> students = new ArrayList<Student>();

    static {
        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setId(i + 1);
            student.setName("测试账户" + (i + 1));
            student.setAge(random.nextInt(100));
            student.setPhone(String.valueOf(13438355172L + random.nextInt(438355172)));
            student.setEmail("abc" + random.nextInt(200000) + "@uoko.com");
            student.setCreateDate(new Date());
            students.add(student);
        }
    }

    public static void add(Student student) {
        student.setCreateDate(new Date());
        student.setId(students.size() + 1);
        students.add(student);
    }

    public static void edit(Student student) {
         Student entity = null;
         for (Student item: students){
             if (item.getId() == student.getId()){
                 entity = item;
                 break;
             }
         }

        entity.setName(student.getName());
        entity.setPhone(student.getPhone());
        entity.setAge(student.getAge());
        entity.setEmail(student.getEmail());
    }

    public static void delete(int id) {
        Iterator<Student> studentIterable = students.iterator();
        while (studentIterable.hasNext()) {
            Student item = studentIterable.next();
            if (item.getId() == id) {
                students.remove(item);
                break;
            }
        }
    }

    public static Student query(int id) {
        for (Student item : students) {
            if (item.getId() == id) {
                return item;
            }
        }
        return null;
    }

    public static List<Student> query(StudentQueryFilter filter) {
        List<Student> results = new ArrayList<Student>(students.size());
        for (Student item : students) {
            results.add(item);
        }

        Iterator<Student> studentIterable = results.iterator();

        while (studentIterable.hasNext()) {
            Student item = studentIterable.next();

            if (filter.getName() != null && !filter.getName().equals("")) {
                if (!item.getName().contains(filter.getName())) {
                    studentIterable.remove();
                    continue;
                }
            }

            if (filter.getPhone() != null && !filter.getPhone().equals("")) {
                if (!item.getPhone().contains(filter.getPhone())) {
                    studentIterable.remove();
                    continue;
                }
            }

            if (filter.getEmail() != null && !filter.getEmail().equals("")) {
                if (!item.getEmail().contains(filter.getEmail())) {
                    studentIterable.remove();
                    continue;
                }
            }

            if (filter.getMinAge() != null) {
                if (item.getAge() < filter.getMinAge()) {
                    studentIterable.remove();
                    continue;
                }
            }

            if (filter.getMaxAge() != null) {
                if (item.getAge() > filter.getMaxAge()) {
                    studentIterable.remove();
                    continue;
                }
            }

            if (filter.getMinDate() != null) {
                if (item.getCreateDate().getTime() < filter.getMinDate().getTime()) {
                    studentIterable.remove();
                    continue;
                }
            }
            if (filter.getMaxDate() != null) {
                if (item.getCreateDate().getTime() > filter.getMaxDate().getTime()) {
                    studentIterable.remove();
                    continue;
                }
            }

        }
 
        int indexStart = (filter.getPageIndex() - 1) * filter.getPageSize();
        indexStart = Math.min(results.size(), indexStart);

        Collections.sort(results, new Comparator<Student>() {
            public int compare(Student o1, Student o2) {
                return new Integer(o2.getId()).compareTo(o1.getId());
            }
        });

        results = results.subList(indexStart, Math.min(results.size(), indexStart + filter.getPageSize()));
        return results;
    }
}
